<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Validate;
class Course extends AdminController
{
    //展示页面
    public  function index()
    {
       $data = Db::name('course')
                  ->select();
        return View('',[
            'data'=>$data]);

    }
    //执行新增
    public function add()
    {
        return view();
    }
    //上传新增数据到数据库
    public function save(Request $request)
    {

       $p = $request->post();
   
       
      //验证
        $rule =[
                'coursename' => 'require|max:20',
                'coursename'=>'chsDash',
                'price'=>'number',
                'desc' => 'require|max:200',
                'desc'=>'chsDash',
               
                 
              ];
              $msg =[
                'coursename.require'=>'课程不能为空',
                'coursename.max'    => '课程名称长度有点长',
                'coursename.chsDash'=>'课程名称只能是汉字、字母、数字和下划线_及破折号-',
                'price.number' => '价格必须是数字',
                'desc.require' =>'描述不能为空',
                'desc.max'     => '描述长度有点长',
                
              ];

             $data = [
             'coursename' =>strtolower($p['coursename']),
              'desc' => $p['desc'],
              'price' => $p['price'],
              'display' => $p['display']
       ];

              $validate = new Validate($rule, $msg);
            if (!$validate->check($data)) {
            $this->error($validate->getError());
            }



       $a = Db::field('coursename')
               ->name('course')
               ->select();
        $b=[];
        foreach ($a as $k => $v) {
          $b[]=$v['coursename'];
        }
        if(in_array($data['coursename'],$b)){
          return  $this->error('您添加的课程已经存在,请重新添加');
        }
      //3.输入的价格不能为负数
        if($data['price'] < 0){
          return  $this->error('输入的价格不能为负数');
        }
       

        // var_dump($b);die;
       $res = Db::name('course')
               ->data($data)
               ->insert();
         
    if($res > 0){
        return $this ->success('添加成功',url('admin/course/index'));
      }else{
        return $this->getError('添加失败,请重试',url('admin/course/add'));
      }

    }

    
    //编辑课程(edit)
    public function edit($id)
    {
        //查数据
        $data = Db::name('course')
                  ->where('id ='.$id)
                  ->find();
       return view('',[
         'data'=>$data
          ]);
    }

    //更新编辑的课程的信息
    public  function update(Request $request,$id)
    {
       
        $p = $request->post();
// var_dump($p);die;
       
         //验证
              $rule =[
                      'coursename' => 'require|max:20|chsDash',
                      'price'=>'number',
                      'desc' => 'require|max:200|chsDash',
                    ];
              $msg =[
                'coursename.require'=>'课程不能为空',
                'coursename.max'    => '课程名称长度有点长',
                 'coursename.chsDash'=>'课程名称只能是汉子,字母,数字和下划线_及破折号-',
                'price.number' => '价格必须是数字',
                'desc.require' =>'描述不能为空',
                'desc.max'     => '描述长度有点长',
                'desc.chsDash'=>'描述只能是汉字、字母、数字和下划线_及破折号-',
              ];

             $data =[
                   'coursename' =>strtolower($p['coursename']),
                    'desc' => $p['desc'],
                    'price' => $p['price'],
                    'display' => $p['display'],
                    ];

      $validate = new Validate($rule,$msg);
      if(!$validate->check($data)){
      $this->error($validate->getError());
      }
      
         // var_dump($data);die;
        $res = Db::name('course')
                    ->where('id',$id)
                    ->update($data);
         // var_dump($res);die;
        if($res > 0){
           $this->success('编辑成功',url('admin/course/index'));
        }else{
       $this->error('编辑失败,请重试');
    } 
}


       public function delete($id)
     {  
        
        $data = Db::name('classdate')
                  ->where(['coid'=>input('id')])
                  ->select();
         if(!empty($data)){
              $arr['status'] =3;
              $arr['msg'] = '该课程下有班期不能删除!';
              return $arr;
            }
        $res = Db::name('course')
                   ->where(['id'=>input()['id']])
                  ->delete();
        
        if($res){
            $arr['status']=1;
            $arr['msg']='删除成功';
            return $arr;
        }else{
            $arr['status']=2;
            $arr['msg']='删除失败';
            return $arr;
        }
     }










}
